new Vue({
	el: '#mainBody',
	data: function() {
		var validatePass = function(rule, value, callback) {
	        if (value === 'null') {
	          callback(new Error('输入非法值，请修改！'));
	        }else{
	        	callback();
	        }
	  };
	  var validateNumber = function(rule, value, callback){
	        if (value.trim() !='') {
	        	if (!/^[0-9]+(.[0-9]{1,2})?$/.test(value)) {
	        		callback(new Error('请输入数字值(小数精确到小数点后两位)'));
		          }else{
			        	callback();
			        }
	        }else{
	        	callback();
	        }
	  };
		return {
			currentpage : 1,// 当前页数
			pageBean : [// 分页参数列表
			],
			viewTitle: '', //查看修改title
			showDialog: false,
			isSearch: false,
			searchForm: { //查询条件
				suppliersName: '', //供应商名称
				suppliersService: '', //服务项目
				suppliersDate: '' //服务提供时间
			},
			suppliers: [ //供应商名称
			],
			suppliersServices: [ //服务项目
			],
			suppliersForm: { //查看修改的数据
				suppliersName: '',
				suppliersService: '',
				suppliersDate: [],
				suppliersPrice: '',
				suppliersNodes: ''
			},
			showRules: { //form 验证
				suppliersName: [
					{
						required: true,
						message: '请选择正确的供应商名称',
						trigger: 'change'
					}
				],
				suppliersService: [
					{
						required: true,
						message: '请选择正确的服务项目',
						trigger: 'change'
					}
				],
				suppliersDate: [
					{
						required: true,
						message: '请输入服务提供时间',
						trigger: 'blur'
					}
				],
				suppliersPrice: [
					{
						required: true,
						message: '请输入服务发生金额',
						trigger: 'blur'
					},
					{ validator: validateNumber, trigger: 'blur' }
				],
				suppliersNodes: [
				   { validator: validatePass, trigger: 'blur' }
				                 ]
			},
			isDisabled: false, //form isDisabled
			suppliersServiceList: [ //供应商服务查询结果列表
			]
		}
	},
	created : function() {
		this.findSSR();
		this.loadOption();
	},
	methods: {
		/**
		 * 下一页上一页按钮
		 */
		handleCurrentChange : function(val) {
			this.currentpage = val;
			this.findSSR();
		},
		search : function() {
			this.currentpage = 1;
			this.isSearch=true;
			this.findSSR();
		},
		findSSR : function() {
			var _this = this;
			var data = "{}";
			if(_this.isSearch){
				data = JSON.stringify(_this.searchForm);
			}
			$.ajax({
				url : '/ssr/findSSR',
				type : 'post',
				data : {
					"data" : data,
					"page" : _this.currentpage
				},
				dataType : 'json',
				success : function(res) {
					_this.exhibition(res);
				},
				error : function(err) {
					if(err.status == 403){
                        errorMessage(_this.$message, "没有权限，请先申请该权限！");
                        return;
                    }
					if(err.responseJSON.message=="session clean"){
                        errorMessage(_this.$message, '登陆已失效重新登陆!');
                        toLogin();
                        return;
                    }
					errorMessage(_this.$message, '获取供应商服务记录列表出错！')
				}
			})
		},
		addSuppliersService: function() { //新建供应商服务
			this.viewTitle = '新增服务记录'
			this.showDialog = true
			this.suppliersForm = {}
			this.isDisabled = false
		},
		closeDialog: function(formName) { //关闭清除校验结果
			this.$refs[formName].clearValidate()
		},
		resetSearchForm: function(formName) { //清空搜索条件
			this.$refs[formName].resetFields();
			this.isSearch=false;
			this.findSSR();
		},
		viewItem: function(index) { //查看
			this.showDialog = true
			this.suppliersForm = this.suppliersServiceList[index]
			this.viewTitle = '查看服务记录'
			this.isDisabled = true
		},
		changeItem: function(index) { //修改
			this.showDialog = true
			this.selectedForm = index
			this.suppliersForm = JSON.parse(JSON.stringify(this.suppliersServiceList[index]))
			this.viewTitle = '修改服务记录'
			this.isDisabled = false
		},
		deleteItem: function(index) { //删除
			var _this = this
			var id = this.suppliersServiceList[index].id;
			this.$confirm('确认要删除该条记录？', '删除提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(function () {
				$.ajax({
	                url: '/ssr/delSSR',
	                data: {"id":id},
	                type: 'post',
	                dataType: 'json',
	                success: function (res) {
	                	successMessage(_this.$message, '删除成功');
	                	_this.findSSR();
	                },
	                error: function (err) {
	                	if(err.status == 403){
	                        errorMessage(_this.$message, "没有权限，请先申请该权限！");
	                        return;
	                    }
	                	if(err.responseJSON.message=="session clean"){
	                        errorMessage(_this.$message, '登陆已失效重新登陆!');
	                        toLogin();
	                        return;
	                    }
	                	errorMessage(_this.$message, '删除失败，联系管理员!');
	                }
	            });
            });
		},
		saveForm: function(formName) { //新建保存
			var _this = this
			this.$refs[formName].validate(function(valid) {
				if (valid) {
					$.ajax({
		                url: '/ssr/saveOrupdateSSR',
		                data: {"data":JSON.stringify(_this.suppliersForm)},
		                type: 'post',
		                dataType: 'json',
		                success: function (res) {
		                	var update = '';
		                    _this.$alert('','保存成功', {
		                        confirmButtonText: '确定',
		                        type:'success',
		                        center:'true'
		                    });
	                		update = 1;
	                		_this.findSSR();
	                		_this.showDialog = false
		                },
		                error: function (err) {
		                	if(err.status == 403){
                                errorMessage(_this.$message, "没有权限，请先申请该权限！");
                                return;
                            }
		                	if(err.responseJSON.message=="session clean"){
		                        errorMessage(_this.$message, '登陆已失效重新登陆!');
		                        toLogin();
		                        return;
		                    }
		                	_this.$alert('','保存失败', {
		                        confirmButtonText: '确定',
			                	type:'error',
			                	center:'true'
			            	});
		                }
		            });
				} else {
					return false
				}
			})
		},
		/*updateForm: function(formname) { //修改保存
			this.showDialog = false
			this.suppliersForm.suppliersDate = JSON.parse(JSON.stringify(this.suppliersForm.suppliersDate))
			this.$set(this.suppliersServiceList, this.selectedForm, this.suppliersForm)
			successMessage(this.$message, '修改成功')
		}*/
		/*
		 * 页面初始化,加载下拉选项
		 */
		loadOption : function() {
			var _this = this;
			$.ajax({
				url : '/ssr/loadOption',
				type : 'get',
				dataType : 'json',
				success : function(res) {
					_this.suppliers = res.data.suppliers;// 供应商名称
					_this.suppliersServices = res.data.suppliersServices;// 服务项目
				},
				error : function(err) {
					if(err.responseJSON.message=="session clean"){
                        errorMessage(_this.$message, '登陆已失效重新登陆!');
                        toLogin();
                        return;
                    }
					errorMessage(_this.$message, '初始化下拉列表出错！')
				}
			})
		},
		/**
		 * 
		 * 处理页面数据展示
		 */
		exhibition : function(res) {
			var _this = this;
			_this.suppliersServiceList = res.data.beanList
			var page = {}
			page['totalNumber'] = res.data.totalNumber
			page['totalPage'] = res.data.totalPage
			_this.pageBean = page
		},
		 /**
         * 下载查询数据
         */
        downloadsuppliers: function(){
        	var _this = this;
        	var data = "{}";
			if(_this.isSearch){
				data = JSON.stringify(_this.searchForm);
			}
        	window.location.href='/ssr/downloadssr?data='+encodeURIComponent(data);
        },
	}
})